package com.tudouji.project.permission.controller;

import com.flow.service.pojo.flow.FlowTempNode;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.flow.domain.Flowtempnode;
import com.tudouji.project.flow.service.IFlowtempnodeService;
import com.tudouji.project.permission.domain.Permissiondef;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.service.IPermissiondefService;
import com.tudouji.project.permission.service.ISalespermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限定义Controller
 *
 * @author wf
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/permission/permissiondef")
public class PermissiondefController extends BaseController
{
    @Autowired
    private IPermissiondefService permissiondefService;

    @Autowired
    private ISalespermissionService salespermissionService;

    @Autowired
    private IFlowtempnodeService flowtempnodeService;

    /**
     * 查询权限定义列表
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:list')")
    @GetMapping("/list")
    public TableDataInfo list(Permissiondef permissiondef)
    {
        startPage();
        List<Permissiondef> list = permissiondefService.selectPermissiondefList(permissiondef);
        return getDataTable(list);
    }

    /**
     * 导出权限定义列表
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:export')")
    @Log(title = "权限定义", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Permissiondef permissiondef)
    {
        List<Permissiondef> list = permissiondefService.selectPermissiondefList(permissiondef);
        ExcelUtil<Permissiondef> util = new ExcelUtil<Permissiondef>(Permissiondef.class);
        return util.exportExcel(list, "permissiondef");
    }

    /**
     * 获取权限定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(permissiondefService.selectPermissiondefById(guid));
    }

    /**
     * 新增权限定义
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:add')")
    @Log(title = "权限定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Permissiondef permissiondef)
    {
        Permissiondef pd = new Permissiondef();
        pd.setCode(permissiondef.getCode());
        List<Permissiondef> permissiondefList = this.permissiondefService.selectPermissiondefList(pd);
        if(permissiondefList.size() > 0){
            return AjaxResult.error("编码已经存在，无法重复添加");
        }
        return toAjax(permissiondefService.insertPermissiondef(permissiondef));
    }

    /**
     * 修改权限定义
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:edit')")
    @Log(title = "权限定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Permissiondef permissiondef)
    {
        Permissiondef pd = new Permissiondef();
        pd.setGuid(permissiondef.getGuid());
        pd.setCode(permissiondef.getCode());
        List<Permissiondef> permissiondefList = this.permissiondefService.selectPermissiondefList(pd);
        if(permissiondefList.size() > 0){
            return AjaxResult.error("编码已经存在，无法重复添加");
        }
        return toAjax(permissiondefService.updatePermissiondef(permissiondef));
    }

    /**
     * 删除权限定义
     */
    @PreAuthorize("@ss.hasPermi('permission:permissiondef:remove')")
    @Log(title = "权限定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        boolean flag = true;
        for(String guid : guids){
            Permissiondef permissiondef = this.permissiondefService.selectPermissiondefById(guid);
            Salespermission salespermission = new Salespermission();
            salespermission.setPermission(permissiondef.getCode());
            List<Salespermission> salespermissionList = this.salespermissionService.selectSalespermissionList(salespermission);
            if(salespermissionList.size() > 0){
                flag = false;
            }else{
                FlowTempNode flowtempnode = new FlowTempNode();
                flowtempnode.setPermission(permissiondef.getCode());
                List<FlowTempNode> flowtempnodeList = this.flowtempnodeService.selectFlowtempnodeList(flowtempnode);
                if(flowtempnodeList.size() > 0){
                    flag = false;
                }
            }
        }
        if(flag){
            return toAjax(permissiondefService.deletePermissiondefByIds(guids));
        }else{
            return AjaxResult.error("选中的数据中有被使用，无法删除");
        }

    }


    @GetMapping("/getPermissionDeflist")
    public AjaxResult getPermissionDeflist(Permissiondef permissiondef)
    {
        List<Permissiondef> list = permissiondefService.selectPermissiondefList(permissiondef);
        return AjaxResult.success(list);
    }

    @GetMapping("/getDictEmnu")
    public AjaxResult getDictEmnu(Salespermission salespermission)
    {
        List<Permissiondef> list = permissiondefService.getDictEmnu(salespermission);
        return AjaxResult.success(list);
    }

}
